home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Libraries / tcl7.4b3 / doc / upvar.n < prev    next >
Encoding:
Text File  |  1995-03-10  |  2.5 KB  |  70 lines

  1. '\"
  2. '\" Copyright (c) 1993 The Regents of the University of California.
  3. '\" Copyright (c) 1994 Sun Microsystems, Inc.
  4. '\"
  5. '\" See the file "license.terms" for information on usage and redistribution
  6. '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  7. '\" 
  8. '\" @(#) upvar.n 1.5 95/03/10 13:47:56
  9. '\" 
  10. .so man.macros
  11. .HS upvar tcl
  12. .BS
  13. '\" Note:  do not modify the .SH NAME line immediately below!
  14. .SH NAME
  15. upvar \- Create link to variable in a different stack frame
  16. .SH SYNOPSIS
  17. \fBupvar \fR?\fIlevel\fR? \fIotherVar myVar \fR?\fIotherVar myVar \fR...?
  18. .BE
  19.  
  20. .SH DESCRIPTION
  21. .PP
  22. This command arranges for one or more local variables in the current
  23. procedure to refer to variables in an enclosing procedure call or
  24. to global variables.
  25. \fILevel\fR may have any of the forms permitted for the \fBuplevel\fR
  26. command, and may be omitted if the first letter of the first \fIotherVar\fR
  27. isn't \fB#\fR or a digit (it defaults to \fB1\fR).
  28. For each \fIotherVar\fR argument, \fBupvar\fR makes the variable
  29. by that name in the procedure frame given by \fIlevel\fR (or at
  30. global level, if \fIlevel\fR is \fB#0\fR) accessible
  31. in the current procedure by the name given in the corresponding
  32. \fImyVar\fR argument.
  33. The variable named by \fIotherVar\fR need not exist at the time of the
  34. call;  it will be created the first time \fImyVar\fR is referenced, just like
  35. an ordinary variable.
  36. .VS
  37. \fIMyVar\fR may not refer to an element of an array, but \fIotherVar\fR
  38. may refer to an array element.
  39. .VE
  40. \fBUpvar\fR returns an empty string.
  41. .PP
  42. The \fBupvar\fR command simplifies the implementation of call-by-name
  43. procedure calling and also makes it easier to build new control constructs
  44. as Tcl procedures.
  45. For example, consider the following procedure:
  46. .DS
  47. .ta 1c 2c 3c
  48. \fBproc add2 name {
  49.     upvar $name x
  50.     set x [expr $x+2]
  51. }
  52. .DE
  53. \fBAdd2\fR is invoked with an argument giving the name of a variable,
  54. and it adds two to the value of that variable.
  55. Although \fBadd2\fR could have been implemented using \fBuplevel\fR
  56. instead of \fBupvar\fR, \fBupvar\fR makes it simpler for \fBadd2\fR
  57. to access the variable in the caller's procedure frame.
  58. .PP
  59. .VS
  60. If an upvar variable is unset (e.g. \fBx\fR in \fBadd2\fR above), the
  61. \fBunset\fR operation affects the variable it is linked to, not the
  62. upvar variable.  There is no way to unset an upvar variable except
  63. by exiting the procedure in which it is defined.  However, it is
  64. possible to retarget an upvar variable by executing another \fBupvar\fR
  65. command.
  66. .VE
  67.  
  68. .SH KEYWORDS
  69. context, frame, global, level, procedure, variable
  70.